function Connection = kineticsAnalysis(varargin)
% CONNECTION = KINETICSANALYSIS(CONNECTION) calculates the risetime of the
% first EPSP. It also measures the decay time constant tau for the first
% EPSP. Use this program after using CONNECTION = MULTIRECSUMMARIZE.
% written by Taro Kiritani, tarokiritani2008@u.northwestern.edu
% 10/26/2010

if strcmp(varargin{1,1},'forward')
    Connection = get(gcf,'UserData');
    k = str2double(get(gcf,'Tag')) + 1;
    if k > length(Connection)
        k = k-1;
    end
    set(gcf,'Tag',num2str(k))
    
elseif strcmp(varargin{1,1}, 'back')
    Connection = get(gcf,'UserData');
    k = str2double(get(gcf,'Tag')) - 1;
    if k == 0
        k = 1;
    end
    set(gcf,'Tag',num2str(k))
else
    kineticFig = figure;
    Connection = varargin{1,1};
    set(gcf,'UserData',Connection);
    set(gcf,'Tag','1')
    if nargin == 2
        k = varargin{1,2};
    else
        k=1;
    end  
end
    

    
    [spikePeak,Ipre] = max(Connection(k).YDatapre(2000:2100)); % find pre spike
    epspTrace = Connection(k).YDataBaselineSubtracted(1999 + Ipre:Ipre + 2499); % extract epsp trace
    epspTraceNorm = Connection(k).YDataNormalized(1999 + Ipre:Ipre + 2499);
    
    % after this point, only look at extracted traces.
    
    plot(epspTrace) %plot first epsp
    
    % 20-80% rise time of epsp.
    rise80 = find(epspTraceNorm > 0.8,1);
    offset = 0;
    rise20 = find(epspTraceNorm(1 + offset:400) > 0.2,1) + offset; % avoid the artifact caused by stimulation 
    line([rise80 rise80], [0 epspTrace(rise80)],'Color','r')
    line([rise20 rise20], [0 epspTrace(rise20)],'Color','r')
    
    % decay time analysis.
    % take the trace to fit. 350 ms before the onset of the second stim.
    decayTrace = Connection(k).YDataBaselineSubtracted(2150:2500);
    [A, lambda] = fitExp([1:351],decayTrace);
    hold on;
    f = @(x) A(1)*exp(-A(2)*(x-150+Ipre));
    fh = ezplot(f,[0 500]);
    set(fh,'Color',[1 0 0])
    hold off;
    disp(Connection(k).snRatio)
    
    ylim([0 A(1)*2])
    
    % parameters are stored in Connection.
    Connection(k).rise2080 = (rise80 - rise20)/10; %[ms]
    Connection(k).decayConstant = 0.1/(A(2)); %[ms]
    Connection(k).expAmp = A(1);
    set(gcf,'UserData',Connection);
    
    uicontrol('Style', 'pushbutton', 'String', '<<', 'Units', 'normalized', ...
    'Position', [.92 .93 .04 .07], 'Callback', 'kineticsAnalysis(''back'')');
    uicontrol('Style', 'pushbutton', 'String', '>>', 'Units', 'normalized', ...
    'Position', [.96 .93 .04 .07], 'Callback', 'kineticsAnalysis(''forward'')');
%     uicontrol('Style', 'pushbutton', 'String', 'New', 'Units', 'normalized', ...
%     'Position', [.92 .86 .08 .07], 'Callback', 'qvPaired(''select new file'')');
%     uicontrol('Style', 'text', 'String', ...
%     ['epoch ' num2str(currentTrace.header.xsg.xsg.epoch)], ...
%     'Units', 'normalized', 'Position', [.01 .96 .1 .03]);    
end